package com.cjd.common.utils

import android.app.Activity
import android.content.Context
import android.content.pm.ActivityInfo
import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.cjd.common.R
import java.lang.reflect.Field

/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/9/24
 * description
 */
object DisplayUtil {

    var SCREEN_WIDTH_PIXELS: Int = 0
    var SCREEN_HEIGHT_PIXELS: Int = 0
    var SCREEN_DENSITY: Float = 0.toFloat()
    var SCREEN_WIDTH_DP: Int = 0
    var SCREEN_HEIGHT_DP: Int = 0
    var SCREEN_STATUS_BAR_HEIGHT_PIXELS: Int = 0
    var ACTIVITY_TOOL_BAR_HEIGHT_PIXELS: Int = 0

    fun init(context: Context?) {
        if (context == null) {
            return
        }
        val dm = DisplayMetrics()
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        wm.defaultDisplay.getMetrics(dm)
        SCREEN_WIDTH_PIXELS = dm.widthPixels
        SCREEN_HEIGHT_PIXELS = dm.heightPixels
        SCREEN_DENSITY = dm.density
        SCREEN_WIDTH_DP = (SCREEN_WIDTH_PIXELS / dm.density).toInt()
        SCREEN_HEIGHT_DP = (SCREEN_HEIGHT_PIXELS / dm.density).toInt()
        ACTIVITY_TOOL_BAR_HEIGHT_PIXELS =
            context.resources.getDimensionPixelOffset(R.dimen.base_toolbar_height)
        SCREEN_STATUS_BAR_HEIGHT_PIXELS =
            context.resources.getDimensionPixelOffset(R.dimen.status_bar_height)
    }

    fun dp2px(dp: Float): Int {
        val scale = SCREEN_DENSITY
        return (dp * scale + 0.5f).toInt()
    }

    fun getStatusBarHeight(context: Context): Int {
        var c: Class<*>? = null
        var obj: Any? = null
        var field: Field? = null
        var x = 0
        try {
            c = Class.forName("com.android.internal.R\$dimen")
            obj = c.newInstance()
            field = c.getField("status_bar_height")
            x = Integer.parseInt(field.get(obj).toString())
            return context.resources.getDimensionPixelSize(x)
        } catch (e1: Exception) {
            LogUtils.d("get status bar height fail")
            e1.printStackTrace()
            return dp2px(24f)
        }

    }

    /**
     * 设置隐藏标题栏
     *
     * @param activity
     */
    fun setNoTitleBar(activity: Activity) {
        activity.requestWindowFeature(Window.FEATURE_NO_TITLE)
    }

    /**
     * 设置全屏
     *
     * @param activity
     */
    fun setFullScreen(activity: Activity) {
        activity.window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }

    fun setNavigationHide(activity: Activity) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val uiFlags = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar

                    or View.SYSTEM_UI_FLAG_FULLSCREEN) // hide status bar
            activity.window.decorView.systemUiVisibility = uiFlags
        }
    }

    /**
     * 取消全屏
     *
     * @param activity
     */
    fun cancelFullScreen(activity: Activity) {
        activity.window.clearFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
    }

    fun screenOrientation(activity: Activity, isPortrait: Boolean) {
        if (isPortrait) {
            //竖屏设置 portrait
            activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else {
            //横屏设置
            activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        }
    }
}